import { initSearchConfig, TableSearchPanel, XxpageContainer, XxRangePicker, XxTable } from "@/ui";
import React, { useEffect, useRef, useState } from "react";
import searchFields from "./config/searchFields";
import getActoinPanel from "./config/action";
import getColumns from "./config/columns";
import { custListApi } from "@/api";
import CustDetail from "./custDetail";
import { deleteConfirm, successAlert } from "@/common/methods";



/**@description 客户列表页面 */
const CustomerList = () => {
    const custDetailRef = useRef(null);
    const menuFormRef = useRef(null);
    const [params, setParams] = useState({ ...initSearchConfig(searchFields) })
    const [state, setState] = useState({
        data: [],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找列表 */
    const getList = async () => {
        setLoading(true);
        const { data, code, msg } = await custListApi.getList({ pageSize: 10, current: 1 });
        setState({
            data: [...data?.list],
            totalCount: data?.totalCount || 0
        })
        setLoading(false);

    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }
    useEffect(() => {
        getList()
    }, [params])

    /**  @description 打开编辑窗口 */
    const open = ({ type = "add", record = {} }) => {
        menuFormRef.current.open({ type, record });
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (record) => {
        try {
            await deleteConfirm();
            const { code, msg } = await custListApi.delete(record.id)
        } catch (error) {

        } finally {
            getList()
        }
    }

    /**
     * @description 打开客户详情
     */
    const onHandleClickOpenCustDetail = ({ type = "open", record = Object }) => {
        custDetailRef.current.open({
            type: 'add',
            record: { ...record }
        })
    }


    const [selectedKeys, setSelectedKeys] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectedKeys(newSelectedRowKeys);
    }
    const actionPanel = getActoinPanel({
        open: open
    })
    const tableProps = {
        tablePanelConfig: actionPanel,
        loading,
        columns: getColumns({ open, handleDelete, onHandleClickOpenCustDetail }),
        dataSource: state.data,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current

        },
        rowSelection: {
            type: "checkbox",
            selectedRowKeys: selectedKeys,
            onChange: onSelectChange,
        },
        rowKey: 'id',
        selectedRowKeys: selectedKeys,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {
                    console.log("双击", record);
                }
            }
        },
    }



    return (
        <XxpageContainer>
            <CustDetail onRef={custDetailRef} />
            <TableSearchPanel
                fields={searchFields}
                onSearch={onClickSearch}

            />
            <XxTable  {...tableProps} />

        </XxpageContainer>
    )
}

export default CustomerList;